<?php

namespace App\Http\Controllers\Admin;

use App\Models\Config;

/**
 * 后台配置控制器
 */
class ChannelController extends BaseController
{

    public function __construct(Config $config)
    {
        parent::__construct();
        $this->config = $config;
    }

    /**
     * 配置管理
     */
    public function getIndex()
    {
//        return 1;
         abort(503,'该功能还未开放，敬请期待~');
    }

    /**
     * 新增配置
     */
    public function getAdd()
    {
        return view('admin.config.edit');
    }

    public function postAdd()
    {
        $res = $this->config->addOrUpdate();
        if (!$res) {
            return $this->error($this->config->getError() ? $this->config->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('config', 'getIndex'));
    }

    /**
     * 编辑配置
     */
    public function getEdit()
    {
        $info = $this->config->_getOneById(request('id'));
        if (!$info) {
            return $this->error('获取配置信息错误');
        }
        return view('admin.config.edit', compact('info'));
    }

    public function postEdit()
    {
        $res = $this->config->addOrUpdate();
        if (!$res) {
            return $this->error($this->config->getError() ? $this->config->getError() : '更新失败');
        }
        return $this->success('更新成功', urlAdmin('config', 'getIndex'));
    }

    /**
     * 批量保存配置
     */
    public function postSave()
    {
        $config = request('config');
        if ($config && is_array($config)) {
            foreach ($config as $name => $value) {
                $data[] = ['name' => $name, 'value' => $value];
            }
        }
        $res = $this->config->_updateBatch($data);
        if (!$res) {
            return $this->error($this->config->getError());
        }
        $this->config->generate();
        return $this->success($res);
    }

    /**
     * 删除配置
     */
    public function getDel()
    {
        $res = $this->config->del();
        if (!$res) {
            return $this->error($this->config->getError());
        }
        return $this->success($res);
    }

    // 获取某个标签的配置参数
    public function getGroup()
    {
        $id = request('id', 1);
        $rows = $this->config->_getList(['status' => 1, 'group' => $id], 'id,name,title,extra,value,remark,type', ['sort' => 'desc']);
        return view('admin.config.group', compact('rows'));
    }

    /**
     * 配置排序
     */
    public function getSort()
    {
        $where['status'] = ['>', -1];
        if (request()->has('ids')) {
            $where['id'] = ['in', [request('ids')]];
        }
        if (request()->has('group')) {
            $where['group'] = request('group');
        }
        $rows = $this->config->_getList($where, 'id,title', ['sort' => 'asc', 'id' => 'asc'])->toArray();
        return view('admin.config.sort', compact('rows'));
    }

    public function postSort()
    {
        $ids = request('ids');
        $ids = explode(',', $ids);
        $data = [];
        foreach ($ids as $key => $value) {
            $data[] = ['id' => $value, 'sort' => $key + 1];
        }
        $res = $this->config->_updateBatch($data);
        if (!$res) {
            return $this->error('排序失败！');
        }
        return $this->success('排序成功！');
    }
}